﻿using SD.IOC.Core.Mediators;
using SlamDunk.MES.IAppService.DTOs.Outputs.PlanContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.StockContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SlamDunk.MES.DataDrive.Bussiness
{
    public static class Transformer
    {
        /// <summary>
        /// 获取源单据Id
        /// </summary>
        public static string GetSourceOrderId(SourceOrderType sourceOrderType, string sourceOrderNo, out string sourceOrderName)
        {
            IPlanContract planContract = ResolveMediator.Resolve<IPlanContract>();
            IStockOrderContract stockOrderContract = ResolveMediator.Resolve<IStockOrderContract>();

            IDictionary<string, StockOutOrderInfo> stockOutOrders = stockOrderContract.GetStockOutOrdersByPage(null, null, null, null, null, null, null, null, null, null, null, null, 1, int.MaxValue).Datas.ToDictionary(x => x.Number, x => x);
            IDictionary<string, ProductionPlanInfo> productionPlans = planContract.GetProductionPlansByPage(null, null, null, null, null, null, null, null, null, 1, int.MaxValue).Datas.ToDictionary(x => x.Number, x => x);
            IDictionary<string, ExecutionPlanInfo> executionPlans = planContract.GetExecutionPlansByPage(null, null, null, null, null, null, null, null, null, null, null, null, 1, int.MaxValue).Datas.ToDictionary(x => x.Number, x => x);

            if (sourceOrderType == SourceOrderType.OutboundOrder)
            {
                StockOutOrderInfo stockOutOrder = stockOutOrders[sourceOrderNo];
                sourceOrderName = stockOutOrder.Name;

                return stockOutOrder.Id.ToString();
            }
            if (sourceOrderType == SourceOrderType.ProductionPlan)
            {
                ProductionPlanInfo productionPlan = productionPlans[sourceOrderNo];
                sourceOrderName = productionPlan.Name;

                return productionPlan.Id.ToString();
            }
            if (sourceOrderType == SourceOrderType.ExecutionPlan)
            {
                ExecutionPlanInfo executionPlan = executionPlans[sourceOrderNo];
                sourceOrderName = executionPlan.Name;

                return executionPlan.Id.ToString();
            }

            sourceOrderName = null;
            return sourceOrderNo;
        }

        /// <summary>
        /// 获取储存位置Id
        /// </summary>
        public static string GetStorageLocationId(StorageLocationType storageLocationType, string storageLocationNo)
        {
            IFoundationContract foundationContract = ResolveMediator.Resolve<IFoundationContract>();
            IResourceContract resourceContract = ResolveMediator.Resolve<IResourceContract>();

            #region # 缓存数据

            if (ImportCacheData.FacilityInfos == null)
            {
                ImportCacheData.FacilityInfos = foundationContract.GetFacilities(null, null, null, null).ToList();
            }
            if (ImportCacheData.PersonInfos == null)
            {
                ImportCacheData.PersonInfos = resourceContract.GetPersonsByPage(null, null, null, 1, int.MaxValue).Datas.ToList();
            }
            if (ImportCacheData.TeamInfos == null)
            {
                ImportCacheData.TeamInfos = resourceContract.GetTeamsByPage(null, null, 1, int.MaxValue).Datas.ToList();
            }
            if (ImportCacheData.EnterpriseInfos == null)
            {
                ImportCacheData.EnterpriseInfos = resourceContract.GetEnterprisesByPage(null, null, null, 1, int.MaxValue).Datas.ToList();
            }

            #endregion

            if (storageLocationType == StorageLocationType.BusinessDomain ||
                storageLocationType == StorageLocationType.ProductionDomain ||
                storageLocationType == StorageLocationType.StorageDomain)
            {
                Guid storageLocationId = ImportCacheData.FacilityInfos.Single(x => x.Number == storageLocationNo).Id;
                return storageLocationId.ToString();
            }
            if (storageLocationType == StorageLocationType.Person)
            {
                Guid storageLocationId = ImportCacheData.PersonInfos.Single(x => x.Number == storageLocationNo).Id;
                return storageLocationId.ToString();
            }
            if (storageLocationType == StorageLocationType.Team)
            {
                Guid storageLocationId = ImportCacheData.TeamInfos.Single(x => x.Number == storageLocationNo).Id;
                return storageLocationId.ToString();
            }
            if (storageLocationType == StorageLocationType.Customer ||
                storageLocationType == StorageLocationType.Supplier)
            {
                Guid storageLocationId = ImportCacheData.EnterpriseInfos.Single(x => x.Number == storageLocationNo).Id;
                return storageLocationId.ToString();
            }

            return storageLocationNo;
        }
    }
}
